package com.czxy.demo01;

import com.sun.media.sound.SoftTuning;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

public class Test12 {
    public static void main(String[] args) {
        //准备数据            语  数  外
        String[][] all = {
                {"张三","37","89","5"},
                {"李四","95","41","79"},
                {"王五","60","84","29"},
                {"赵六","87","46","86"}
        };

        System.out.println("所有同学的数学成绩:");
        printMathScore(all);
        System.out.println("所有同学的数学成绩总和:");
        System.out.println(getCountMathScore(all));
        System.out.println("所有同学的数学成绩平均分:");
        System.out.println(getAvgMathScore(all));
        System.out.println("数学成绩超过平均分的人：");
        getAvgName(all);

        System.out.println("数学成绩超过平均分的人数："+getAvgCount(all));


        getMaxAvg(all);
        System.out.println("前三名是:");
        Arrays.sort(all, new Comparator<String[]>() {
            @Override
            public int compare(String[] o1, String[] o2) {
                int num1 = Integer.parseInt(o1[1]);
                int num2 = Integer.parseInt(o2[1]);

                return num2-num1;
            }
        });

        int count =0 ;
        for (String[] strings : all) {
            if(count==3){
                break;
            }
            System.out.println(strings[1]);
            count++;
        }


        System.out.println("不及格的科数小于等于1门的同学，且总成绩大于180分：");
        for (String[] strings : all) {
            int count1 = 0;
            int sum = 0 ;
            for (int i = 1; i <strings.length ; i++) {
                if(Integer.parseInt(strings[i])<60){
                    count1++;
                }
                sum+=Integer.parseInt(strings[i]);
            }

            if(count1>=1 && sum>180){
                System.out.println(strings[0]);
            }
        }


    }

    public static void printMathScore(String [][]all){
        for (int i = 0; i <all.length ; i++) {
            System.out.println(all[i][2]);
        }
    }

    public static int getCountMathScore(String [][]all){
        int sum = 0;
        for (int i = 0; i <all.length ; i++) {
            sum+=Integer.parseInt(all[i][2]);
        }
        return sum;
    }
    public static int getAvgMathScore  (String [][]all){
        int mathSum = getCountMathScore(all);
        return mathSum/all.length;
    }

    public static void getAvgName (String [][]all){

        int avgMathScore = getAvgMathScore(all);

        for (String[] strings : all) {

            int num = Integer.parseInt(strings[2]);
            if(avgMathScore>=num){
                System.out.println(strings[0]);
            }
        }
    }

    public static int getAvgCount(String [][]all){
        int avgMathScore = getAvgMathScore(all);
        int count = 0;
        for (String[] strings : all) {

            int num = Integer.parseInt(strings[2]);
            if(avgMathScore>=num){
                count++;
            }
        }
        return count;
    }

    public static int getChineseAvg(String [][]all){

        int sum = 0;

        for (String[] strings : all) {
            sum+=Integer.parseInt(strings[1]);
        }

        return sum/all.length;
    }

    public static int getEnglishAvg(String [][]all){

        int sum = 0;

        for (String[] strings : all) {
            sum+=Integer.parseInt(strings[3]);
        }

        return sum/all.length;
    }

    public static void getMaxAvg(String [][]all){
        Map<Integer,String> map = new HashMap<>();

        map.put(getAvgMathScore(all),"数学");
        map.put(getEnglishAvg(all),"英语");
        map.put(getChineseAvg(all),"语文");

        int max = 0;
        for (Integer integer : map.keySet()) {
            if(max < integer){
                max = integer;
            }
        }

        System.out.println("平均成绩最高的学科是 ："+map.get(max));
    }
}
